package xyz.yuzegod.megawalls.classes.normal.arcane;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.LocationUtils;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MainSkill extends Skill
{
    private static final Set<Material> set;
    
    public MainSkill(final Classes classes) {
        super("奥术激光", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 1.0;
            }
            case 2: {
                return 1.25;
            }
            case 3: {
                return 1.5;
            }
            case 4: {
                return 1.75;
            }
            case 5: {
                return 2.0;
            }
            default: {
                return 1.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7发射一束激光,造成§a" + this.getAttribute(level) + "§7点伤害");
            lore.add("   §7也可以使用此技能进行挖掘。");
            lore.add(" ");
            lore.add("§7冷却时间:§a1秒");
            return lore;
        }
        lore.add(" §8▪ §7发射一束激光,造成§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7点伤害");
        lore.add("   §7也可以使用此技能进行挖掘。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        if (Arcane.skillCooldown.getOrDefault(gamePlayer, 0) > 0) {
            player.sendMessage("§c技能正在冷却中！");
            return false;
        }
        final List<Player> damaged = new ArrayList<Player>();
        for (final Block block : player.getLineOfSight(MainSkill.set, 12)) {
            ParticleEffect.REDSTONE.display(0.0f, 0.0f, 0.0f, 0.0f, 3, block.getLocation(), 10.0);
            block.getWorld().playSound(block.getLocation(), Sound.DIG_STONE, 1.0f, 1.0f);
            for (final Block block2 : LocationUtils.getCube(block.getLocation(), 3)) {
                if (!MegaWalls.getInstance().getGame().isUnbreakable(block2.getLocation()) && ((block2.getType() != Material.FURNACE && block2.getType() != Material.BURNING_FURNACE && block2.getType() != Material.TRAPPED_CHEST) || gamePlayer.isProtectedBlock(block2))) {
                    if (block2.getType() == Material.BEDROCK) {
                        continue;
                    }
                    block2.breakNaturally();
                }
            }
            for (final Player player2 : this.getNearbyPlayers(block.getLocation(), player, 2)) {
                if (damaged.contains(player2)) {
                    continue;
                }
                player2.damage(this.getAttribute(this.getPlayerLevel(gamePlayer)), (Entity)player);
                damaged.add(player2);
            }
        }
        Arcane.skillCooldown.put(gamePlayer, 1);
        return true;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.getClasses().getNameColor() + "§l" + this.getName() + " " + ((Arcane.skillCooldown.getOrDefault(gamePlayer, 0) == 0) ? ((gamePlayer.getEnergy() == 100) ? "§a§l✓" : "§c§l✕") : ("§c§l" + Arcane.skillCooldown.get(gamePlayer) + "秒"));
    }
    
    private List<Player> getNearbyPlayers(final Location location, final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers(location, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator() && !gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(location) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
    
    static {
        set = new HashSet<Material>();
        for (final Material material : Material.values()) {
            MainSkill.set.add(material);
        }
    }
}
